package com.cgw.game.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.graphics.PathEffect;
import android.graphics.Rect;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatEditText;

/**
 * @类描述 笔记本样式的输入框
 * @作者 YF
 * @创建时间 2019/12/4 15:41
 */
public class MyEditView extends AppCompatEditText {
    private int lineColor;//横线颜色
    private float lineWidth;//横线宽度

    /**
     * 提前需要画的线的条数
     */
    private int drawLine = 15;
    /**
     * 为了不让指针压线太明显，将线下移的像素，可根据字体大小和行间距自己调整
     */
    public int lineDis = 8;
    private Rect mRect;
    Paint mPaint;
    public MyEditView(Context context) {
        super(context);
        init();
    }
    public MyEditView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MyEditView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        //设置默认颜色和横线宽度
        lineColor = Color.RED;//默认蓝色线
        lineWidth = 2f;//宽度为2
        mRect = new Rect();
        //创建画笔
        mPaint = new Paint();
        mPaint.setStrokeWidth(lineWidth);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setColor(lineColor);
        PathEffect effects = new DashPathEffect(new float[]{5,5,5,5},5);
        mPaint.setPathEffect(effects);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        int count = getLineCount();
        int basicline = 0;
        //第一次画第一条线。以后每次输入，换行时仍然检测，继续画线
        for (int i = 0; i < count; i++) {
            int baseline = getLineBounds(i, mRect);
            basicline = baseline;
            canvas.drawLine(mRect.left, baseline + lineDis, mRect.right, baseline + lineDis, mPaint);
        }
        //根据判定条件，画出固定条数的线,从第二套开始画
        if(count < drawLine){
            for (int j = 1; j < drawLine; j++) {
                int baseline = basicline+j*getLineHeight();
                canvas.drawLine(mRect.left, baseline + lineDis, mRect.right, baseline + lineDis, mPaint);
            }
        }
        super.onDraw(canvas);
    }
}
